package com.soar.tbox.util;

import org.jasypt.util.text.BasicTextEncryptor;

import com.soar.tbox.bean.SmsCommand;

/**
 * 短信通知指令解析
 * 
 * @author songjunjie
 * 
 */
public class SmsNotifyCommandAnalyzer {

	public static SmsCommand analyze(String sms) throws Exception {
		if (sms.length() <= 9) {
			throw new Exception("短信通知指令格式不合法:" + sms);
		}
		SmsCommand smscmd = new SmsCommand();
		smscmd.setVersion(sms.substring(0, 2));
		smscmd.setTestFlag(sms.charAt(2));
		smscmd.setType(sms.substring(3, 6));
		smscmd.setCmd(sms.charAt(6));
		smscmd.setEncryptFlag(sms.charAt(7));
		String body = sms.substring(8);
		if (smscmd.getEncryptFlag() == '1') {// 数据是加密的
			body = EncryptHelper.decryptSmsBody(body);
		}
		if (body.length() != 36) {
			throw new Exception("短信通知指令中数据体长度不正确.设备号+分隔符+流水号 长度应该为36:" + sms);
		}
		smscmd.setDeviceNo(body.substring(0, 15));
		smscmd.setSerialNum(body.substring(16));
		return smscmd;
	}
}
